package cn.org.eshow.webapp.action;

import cn.org.eshow.bean.query.ProductOrderQuery;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.model.ProductOrder;
import cn.org.eshow.service.ProductOrderManager;
import cn.org.eshow.util.PageUtil;
import cn.org.eshow.webapp.util.RenderUtil;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Results({@Result(name = "input", location = "add"),
        @Result(name = "list", type = "redirect", location = ""),
        @Result(name = "success", type = "redirect", location = "view/${id}"),
        @Result(name = "redirect", type = "redirect", location = "${redirect}")})
@AllowedMethods({"list", "search", "contact", "waitExecution", "execution","view"})
public class ProductOrderAction extends BaseAction {

    private static final long serialVersionUID = 1L;

    @Autowired
    private ProductOrderManager productOrderManager;

    private List<ProductOrder> productOrders = new ArrayList<ProductOrder>();
    private ProductOrder productOrder = new ProductOrder();
    private ProductOrderQuery query = new ProductOrderQuery();

    private Boolean ifCando;

    /**
     * @return
     */
    public String list() {
        productOrders = productOrderManager.list(query);
        return LIST;
    }

    /**
     * @return
     */
    public String search() {
        Page<ProductOrder> page = productOrderManager.search(query);
        productOrders = page.getDataList();
        saveRequest("page", PageUtil.conversion(page));
        saveRequest("query", query);
        return LIST;
    }


    /**
     * @return
     */
    public String view() {
        if (id != null) {
            productOrder = productOrderManager.get(id);
            productOrder = productOrderManager.save(productOrder);
        } else {
            productOrder = new ProductOrder();
        }
        return REDIRECT;
    }


    /**
     * 联系用户:用户付钱后,客服联系用户。将订单状态升级为"处理中"还是帮用户取消订单
     * @return
     * @throws Exception
     */
    public void contact() throws Exception {
        if (id == null || ifCando == null) {
            RenderUtil.failure("缺少参数");
            return;
        }
        ProductOrder old = productOrderManager.get(id);
        productOrderManager.contact(old, ifCando);
        saveMessage("提交成功");
        RenderUtil.success("提交成功");
        return;
    }

    /**
     * 待施工 :当联系后,得知不需要补单,则由"待量房"变"已量房"
     */
    public void waitExecution(){
        if (id == null) {
            RenderUtil.failure("缺少参数");
            return;
        }
        ProductOrder old = productOrderManager.get(id);
        productOrderManager.waitExecution(old);
        saveMessage("提交成功");
        RenderUtil.success("提交成功");
        return;
    }

    /**
     * 施工: 客服操作施工状态变化
     *  1 待施工（已量房）---》  2待验收（已施工）
     */
    public void execution(){
        if (id == null) {
            RenderUtil.failure("缺少参数");
            return;
        }
        ProductOrder old = productOrderManager.get(id);
        productOrderManager.execution(old);
        saveMessage("提交成功");
        RenderUtil.success("提交成功");
        return;
    }

    public List<ProductOrder> getProductOrders() {
        return productOrders;
    }

    public void setProductOrders(List<ProductOrder> productOrders) {
        this.productOrders = productOrders;
    }

    public ProductOrder getProductOrder() {
        return productOrder;
    }

    public void setProductOrder(ProductOrder productOrder) {
        this.productOrder = productOrder;
    }

    public ProductOrderQuery getQuery() {
        return query;
    }

    public void setQuery(ProductOrderQuery query) {
        this.query = query;
    }

    public Boolean getIfCando() {
        return ifCando;
    }

    public void setIfCando(Boolean ifCando) {
        this.ifCando = ifCando;
    }
}